package zelix.hack.hacks;

import java.lang.reflect.Field;
import zelix.utils.system.Mapping;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import zelix.hack.Hack;
import zelix.hack.HackCategory;
import zelix.value.NumberValue;

public class Timer extends Hack {

	NumberValue ticks;

	public Timer() {
		super("Timer", HackCategory.PLAYER);
		ticks = new NumberValue("Ticks", 2D, 1D, 10D);
		this.addValue(ticks);
	}


	@Override
	public void onEnable() {
		setTickLength(50 / ticks.getValue().floatValue());
		super.onEnable();
	}

	@Override
	public void onDisable() {
		setTickLength(50);
		super.onEnable();
	}

	@Override
	public void onClientTick(TickEvent.ClientTickEvent event){
		setTickLength(50 / ticks.getValue().floatValue());
		super.onClientTick(event);
	}

	private void setTickLength(float tickLength)
	{
		try
		{
			Field fTimer = Minecraft.getMinecraft().getClass().getDeclaredField(Mapping.timer);
			fTimer.setAccessible(true);

			Field fTickLength = net.minecraft.util.Timer.class.getDeclaredField(Mapping.tickLength);
			fTickLength.setAccessible(true);
			fTickLength.setFloat(fTimer.get(Minecraft.getMinecraft()), tickLength);

		}catch(ReflectiveOperationException e)
		{
			throw new RuntimeException(e);
		}
	}

}
